package org.westos.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.westos.dao.ChufangDao;
import org.westos.dao.YaoDao;
import org.westos.pojo.ChuFang;
import org.westos.pojo.ChuFangYao;
import org.westos.pojo.ChufangDto;
import org.westos.pojo.ShowChu;
import org.westos.utils.getPreNum;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

@Service
public class ChufangServiceImpl implements ChufangService{

    private Integer start_id;


    @Autowired
    private ChufangDao chufangDao;
    @Autowired
    private YaoDao yaoDao;
    @Autowired
    private YaoService yaoService;

    @Override
    public Integer addChu(ChufangDto chufangDto) {
        //是否含有重复药品
        boolean repeat_flag = false;
        //处方信息是否完整
        boolean msg_flag;
        //电话信息是否合乎常理
        boolean phone_flag;
        //年龄信息是否合乎常理
        boolean age_flag;
        //付费方式是否在 0-3 之内
        boolean pay_flag;
        //药品信息是否缺省
        boolean null_flag = false;



        //处方
        ChuFang chufang = chufangDto.getChufang();
        //处方对应的药品集合
        List<ChuFangYao> yaos = chufangDto.getChuFangYaoList();
        //检验药品重复时要用到的 set 集合
        HashSet<Integer> hashSet = new HashSet<>();

        //设置处方id，数据库最大 id+1
        Integer wait_max = chufangDao.getMax();
        Integer checked_max = chufangDao.getMax2();
        Integer all_max = chufangDao.getMax3();

        if(wait_max==null){
            wait_max=0;
        }
        if(checked_max==null){
            checked_max=0;
        }
        if(all_max==null){
            all_max=0;
        }
        Integer _max = wait_max>checked_max?wait_max:checked_max;

        Integer max = _max>all_max?_max:all_max;

        chufang.setPrescription_id(max+1);

        //设置处方编号
        chufang.setPrescription_num(getPreNum.getCode());

        //设置处方日期
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        String date_time = sf.format(date);
        chufang.setDate_time(date_time);


        //校验是否含有重复药品
        for (ChuFangYao yao : yaos) {
            repeat_flag = !hashSet.add(yao.getDrugs_id());
            //如果 repeat_flag 为true，则 break掉循环
            if(repeat_flag)
                break;
        }

        //校验处方信息完整型
        msg_flag= chufang.getPrescription_id()!=null&&chufang.getPrescription_num()!=null&&chufang.getPay()!=null
                &&chufang.getPatient_name()!=null&&chufang.getPatient_age()!=null&&chufang.getPatient_weight()!=null&&
                chufang.getAllergy()!=null&&chufang.getDiagnosis()!=null&&chufang.getAddress()!=null&&chufang.getPhone()
                !=null&&chufang.getDate_time()!=null&&chufang.getDoctor()!=null&&chufang.getAudited()!=null&&chufang
                .getAllocate()!=null&&chufang.getDistribute()!=null&&chufang.getState()!=null;


        //测试逻辑
        if(repeat_flag){
            //处方药品信息重复，返回值是 11
            return 11;
        }else {
            if(!msg_flag){
                //9.处方信息不完整，返回值是 9
                return 9;
            }else {
                //判断处方信息是否符合常理
                   //1.校验手机号是否符合
                String phone = chufang.getPhone();
                String phone_regex = "^((13[0-9])|(14[579])|(15([0-3,5-9]))|(16[6])|(17[0135678])|(18[0-9]|19[89]))\\d{8}$";
                phone_flag = phone.matches(phone_regex);
                   //2.检验年龄是否符合常理
                int age = chufang.getPatient_age();
                age_flag = age>=0 && age<=110;
                   //3.校验付费方式是否是：  0：公费；  1：自费；  2：医保；  3：其他
                int pay = chufang.getPay();
                pay_flag = pay>=0 && pay<=3;


                //测试逻辑
                if(phone_flag && pay_flag && age_flag){
                    //1.正常传参（三个需要校验的地方都没有问题并且药品信息没有问题），返回值是 1
                    int result=1;

                    //判断药品信息是否缺省
                    for (ChuFangYao yao : yaos) {
                        null_flag = yaoService.isNull(yao);
                        if(null_flag){
                            //10.处方药品信息缺省，返回值是 10
                            result = 10;
                            break;
                        }
                    }

                    //如果药品信息完整，则添加药品
                    if(!null_flag){
                        for (ChuFangYao yao : yaos) {
                            yaoService.addYao(yao);
                        }
                        //TODO:添加处方,dao层
                        chufangDao.addChuToWait(chufang);
                    }
                    return result;
                }else if(!phone_flag && pay_flag && age_flag){
                    //2.不正常传参（电话信息有问题），返回值是 2
                    return 2;
                }else if(phone_flag && pay_flag && !age_flag){
                    //3.不正常传参（年龄信息有问题），返回值是 3
                    return 3;
                }else if(phone_flag && !pay_flag && age_flag){
                    //4.不正常传参（付费方式有问题），返回值是 4
                    return 4;
                }else if(!phone_flag && pay_flag && !age_flag){
                    //5.不正常传参（电话信息和年龄信息均有问题），返回值是 5
                    return 5;
                }else if(!phone_flag && !pay_flag && age_flag){
                    //6.不正常传参（电话信息和付费方式均有问题），返回值是 6
                    return 6;
                }else if(phone_flag && !pay_flag && !age_flag){
                    //7.不正常传参（年龄信息和付费方式均有问题），返回值是 7
                    return 7;
                }else if(!phone_flag && !pay_flag && !age_flag) {
                    //8.不正常传参（三个信息均有问题），返回值是 8
                    return 8;
                }else {
                    return -1;
                }
            }
        }
    }

    @Override
    public Integer checkChu(Integer prescription_id, Integer state, String audName) {
        if(prescription_id==null||audName==null){
            return 0;
        }else{
            ChuFang chuFang = queryByAudId(prescription_id, audName);
            if(state==1){
                chuFang.setState(state);
                chufangDao.addChecked(chuFang);
                chufangDao.deleteWaitChu(prescription_id);
            }else if(state==2){
                chuFang.setState(state);
                chufangDao.addChuToAll(chuFang);
                chufangDao.deleteWaitChu(prescription_id);
            }
            return 1;
        }
    }

    @Override
    public Integer mixChu(Integer prescription_id,String allName) {
        if(prescription_id==null||allName==null){
            return 0;
        }else{
            List<ChuFangYao> yaos = yaoDao.queryYao(prescription_id);
            for (ChuFangYao yao : yaos) {
                yao.setAll_name(allName);
            }
            ChuFang chuFang = chufangDao.queryCheckedByAllID(prescription_id,allName);
            chufangDao.addChuToAll(chuFang);
            chufangDao.deleteCheckedChu(prescription_id);
            return 1;
        }

    }

    @Override
    public List<ShowChu> queryByDoc(String docName) {
        if(docName==null){
            return null;
        }else {
            ArrayList<ShowChu> chus = new ArrayList<>();
            List<ChuFang> chuFangs = chufangDao.queryAllSheetByDoc(docName);
            for (ChuFang chuFang : chuFangs) {
                ShowChu showChu = new ShowChu();
                showChu.setPrescription_id(chuFang.getPrescription_id());
                showChu.setPrescription_num(chuFang.getPrescription_num());
                showChu.setPatient_name(chuFang.getPatient_name());
                showChu.setPhone(chuFang.getPhone());
                showChu.setDiagnosis(chuFang.getDiagnosis());
                showChu.setDate_time(chuFang.getDate_time());
                showChu.setDoctor(chuFang.getDoctor());
                showChu.setAudited(chuFang.getAudited());
                chus.add(showChu);
            }
            return chus;
        }
    }

    @Override
    public List<ShowChu> queryWaitByDoc(String docName) {
        if(docName==null){
            return null;
        }else {
            ArrayList<ShowChu> chus = new ArrayList<>();
            List<ChuFang> chuFangs = chufangDao.queryWaitByDoc(docName);
            for (ChuFang chuFang : chuFangs) {
                ShowChu showChu = new ShowChu();
                showChu.setPrescription_id(chuFang.getPrescription_id());
                showChu.setPrescription_num(chuFang.getPrescription_num());
                showChu.setPatient_name(chuFang.getPatient_name());
                showChu.setPhone(chuFang.getPhone());
                showChu.setDiagnosis(chuFang.getDiagnosis());
                showChu.setDate_time(chuFang.getDate_time());
                showChu.setDoctor(chuFang.getDoctor());
                showChu.setAudited(chuFang.getAudited());
                chus.add(showChu);
            }
            return chus;
        }
    }

    @Override
    public List<ShowChu> queryCheckedByDoc(String docName) {
        if(docName==null){
            return null;
        }else {
            ArrayList<ShowChu> chus = new ArrayList<>();
            List<ChuFang> chuFangs = chufangDao.queryCheckedByDoc(docName);
            for (ChuFang chuFang : chuFangs) {
                ShowChu showChu = new ShowChu();
                showChu.setPrescription_id(chuFang.getPrescription_id());
                showChu.setPrescription_num(chuFang.getPrescription_num());
                showChu.setPatient_name(chuFang.getPatient_name());
                showChu.setPhone(chuFang.getPhone());
                showChu.setDiagnosis(chuFang.getDiagnosis());
                showChu.setDate_time(chuFang.getDate_time());
                showChu.setDoctor(chuFang.getDoctor());
                showChu.setAudited(chuFang.getAudited());
                chus.add(showChu);
            }
            return chus;
        }
    }

    @Override
    public List<ShowChu> queryByAud(String audName) {
        if(audName==null){
            return null;
        }else {
            ArrayList<ShowChu> chus = new ArrayList<>();
            List<ChuFang> chuFangs = chufangDao.queryAllSheetByAud(audName);
            for (ChuFang chuFang : chuFangs) {
                ShowChu showChu = new ShowChu();
                showChu.setPrescription_id(chuFang.getPrescription_id());
                showChu.setPrescription_num(chuFang.getPrescription_num());
                showChu.setPatient_name(chuFang.getPatient_name());
                showChu.setPhone(chuFang.getPhone());
                showChu.setDiagnosis(chuFang.getDiagnosis());
                showChu.setDate_time(chuFang.getDate_time());
                showChu.setDoctor(chuFang.getDoctor());
                showChu.setAudited(chuFang.getAudited());
                chus.add(showChu);
            }
            return chus;
        }

    }

    @Override
    public List<ShowChu> queryWaitByAud(String audName) {
        if(audName==null){
            return null;
        }else {
            ArrayList<ShowChu> chus = new ArrayList<>();
            List<ChuFang> chuFangs = chufangDao.queryWaitByAud(audName);
            for (ChuFang chuFang : chuFangs) {
                ShowChu showChu = new ShowChu();
                showChu.setPrescription_id(chuFang.getPrescription_id());
                showChu.setPrescription_num(chuFang.getPrescription_num());
                showChu.setPatient_name(chuFang.getPatient_name());
                showChu.setPhone(chuFang.getPhone());
                showChu.setDiagnosis(chuFang.getDiagnosis());
                showChu.setDate_time(chuFang.getDate_time());
                showChu.setDoctor(chuFang.getDoctor());
                showChu.setAudited(chuFang.getAudited());
                chus.add(showChu);
            }
            return chus;
        }
    }

    @Override
    public List<ShowChu> queryCheckedByAud(String audName) {
        if(audName==null){
            return null;
        }else {
            ArrayList<ShowChu> chus = new ArrayList<>();
            List<ChuFang> chuFangs = chufangDao.queryCheckedByAud(audName);
            for (ChuFang chuFang : chuFangs) {
                ShowChu showChu = new ShowChu();
                showChu.setPrescription_id(chuFang.getPrescription_id());
                showChu.setPrescription_num(chuFang.getPrescription_num());
                showChu.setPatient_name(chuFang.getPatient_name());
                showChu.setPhone(chuFang.getPhone());
                showChu.setDiagnosis(chuFang.getDiagnosis());
                showChu.setDate_time(chuFang.getDate_time());
                showChu.setDoctor(chuFang.getDoctor());
                showChu.setAudited(chuFang.getAudited());
                chus.add(showChu);
            }
            return chus;
        }
    }

    @Override
    public List<ShowChu> queryByAll(String allName) {
        if(allName==null){
            return null;
        }else {
            ArrayList<ShowChu> chus = new ArrayList<>();
            List<ChuFang> chuFangs = chufangDao.queryAllSheetByAll(allName);
            for (ChuFang chuFang : chuFangs) {
                ShowChu showChu = new ShowChu();
                showChu.setPrescription_id(chuFang.getPrescription_id());
                showChu.setPrescription_num(chuFang.getPrescription_num());
                showChu.setPatient_name(chuFang.getPatient_name());
                showChu.setPhone(chuFang.getPhone());
                showChu.setDiagnosis(chuFang.getDiagnosis());
                showChu.setDate_time(chuFang.getDate_time());
                showChu.setDoctor(chuFang.getDoctor());
                showChu.setAudited(chuFang.getAudited());
                chus.add(showChu);
            }
            return chus;
        }
    }

    @Override
    public List<ShowChu> queryCheckedByAll(String allName) {
        if(allName==null){
            return null;
        }else {
            ArrayList<ShowChu> chus = new ArrayList<>();
            List<ChuFang> chuFangs = chufangDao.queryCheckedByAll(allName);
            for (ChuFang chuFang : chuFangs) {
                ShowChu showChu = new ShowChu();
                showChu.setPrescription_id(chuFang.getPrescription_id());
                showChu.setPrescription_num(chuFang.getPrescription_num());
                showChu.setPatient_name(chuFang.getPatient_name());
                showChu.setPhone(chuFang.getPhone());
                showChu.setDiagnosis(chuFang.getDiagnosis());
                showChu.setDate_time(chuFang.getDate_time());
                showChu.setDoctor(chuFang.getDoctor());
                showChu.setAudited(chuFang.getAudited());
                chus.add(showChu);
            }
            return chus;
        }
    }


    @Override
    public List<ShowChu> queryByDis(String disName) {
        if(disName==null){
            return null;
        }else {
            ArrayList<ShowChu> chus = new ArrayList<>();
            List<ChuFang> chuFangs = chufangDao.queryAllSheetByDis(disName);
            for (ChuFang chuFang : chuFangs) {
                ShowChu showChu = new ShowChu();
                showChu.setPrescription_id(chuFang.getPrescription_id());
                showChu.setPrescription_num(chuFang.getPrescription_num());
                showChu.setPatient_name(chuFang.getPatient_name());
                showChu.setPhone(chuFang.getPhone());
                showChu.setDiagnosis(chuFang.getDiagnosis());
                showChu.setDate_time(chuFang.getDate_time());
                showChu.setDoctor(chuFang.getDoctor());
                showChu.setAudited(chuFang.getAudited());
                chus.add(showChu);
            }
            return chus;
        }
    }

    @Override
    public ChuFang queryByDocId(Integer prescription_id, String docName) {
        if(prescription_id==null||docName==null){
            return null;
        }else {
            ChuFang chuFang = chufangDao.queryAllSheetByDocID(prescription_id,docName);
            if(chuFang!=null){
                return chuFang;
            }else{
                chuFang = chufangDao.queryCheckedByDocID(prescription_id,docName);
                if(chuFang!=null){
                    return chuFang;
                }else{
                    chuFang = chufangDao.queryWaitByDocID(prescription_id,docName);
                    if(chuFang!=null){
                        return chuFang;
                    }else {
                        return null;
                    }
                }
            }
        }
    }

    @Override
    public ChuFang queryByAudId(Integer prescription_id, String audName) {
        if(prescription_id==null||audName==null){
            return null;
        }else {
            ChuFang chuFang = chufangDao.queryAllSheetByAudID(prescription_id,audName);
            if(chuFang!=null){
                return chuFang;
            }else{
                chuFang = chufangDao.queryCheckedByAudID(prescription_id,audName);
                if(chuFang!=null){
                    return chuFang;
                }else{
                    chuFang = chufangDao.queryWaitByAudID(prescription_id,audName);
                    if(chuFang!=null){
                        return chuFang;
                    }else {
                        return null;
                    }
                }
            }
        }
    }

    @Override
    public ChuFang queryByAllId(Integer prescription_id, String allName) {
        if(prescription_id==null||allName==null){
            return null;
        }else {
            ChuFang chuFang = chufangDao.queryAllSheetByAllID(prescription_id,allName);
            if(chuFang!=null){
                return chuFang;
            }else{
                ChuFang chuFang1 = chufangDao.queryCheckedByAllID(prescription_id,allName);
                if(chuFang1!=null){
                    return chuFang1;
                }else {
                    return null;
                }
            }
        }
    }

    @Override
    public void haha() {
        System.out.println("哈哈");
    }

    
}
